<template>
  <div>
    <!-- 展示列表 -->
    <el-table style="width: 100%" border :data="records">
      <el-table-column type="index" align="center" label="序号" width="80px">
      </el-table-column>
      <!-- 名称 -->
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <!--描述  -->
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <!-- 默认图片 -->
      <el-table-column label="默认图片" width="110px">
        <template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 90px; height: 90px"
          />
        </template>
      </el-table-column>
      <!-- 重量(KG) -->
      <el-table-column prop="weight" label="重量(KG)" width="80px">
      </el-table-column>
      <!-- 价格(元) -->
      <el-table-column prop="price" label="价格(元)" width="80px">
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <!-- 上架 -->
          <el-button
            type="info"
            icon="el-icon-sort-up"
            size="mini"
            v-if="row.isSale == 0"
            @click="upSale(row)"
          ></el-button>
          <!-- 下架 -->
          <el-button
            v-else
            type="success"
            icon="el-icon-sort-down"
            size="mini"
            @click="downSale(row)"
          ></el-button>
          <!-- 编辑 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="explore"
          ></el-button>
          <!-- info -->
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="detail(row)"
          ></el-button>
          <!-- 删除 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器  -->
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <!-- 详情 -->
    <el-drawer :visible.sync="show" :show-close="false" size="30%">
      <!-- 名称 -->
      <el-row :gutter="10" style="padding-left: 10px">
        <el-col :span="8" style="font-size: 10px">名称</el-col>
        <el-col :span="8" style="font-size: 10px">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <!-- 描述 -->
      <el-row :gutter="10" style="margin-top: 10px; padding-left: 10px">
        <el-col :span="8" style="font-size: 10px">描述</el-col>
        <el-col :span="8" style="font-size: 10px">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <!-- 价格 -->
      <el-row :gutter="10" style="margin-top: 10px; padding-left: 10px">
        <el-col :span="8" style="font-size: 10px">价格(元)</el-col>
        <el-col :span="8" style="font-size: 10px">{{ skuInfo.price }}元</el-col>
      </el-row>
      <!-- 平台属性 -->
      <el-row :gutter="10" style="margin-top: 10px; padding-left: 10px">
        <el-col :span="8" style="font-size: 10px">平台属性</el-col>
        <el-col :span="16" style="font-size: 10px">
          <el-tag
            type="success"
            v-for="attr in skuInfo.skuAttrValueList"
            :key="attr.id"
            style="margin-right: 5px"
            >{{ attr.attrId }}-{{ attr.valueId }}</el-tag
          >
        </el-col>
      </el-row>
      <!-- 商品图片 -->
      <el-row :gutter="10" style="margin-top: 20px; padding-left: 10px">
        <el-col :span="8" style="font-size: 10px">商品图片</el-col>
        <el-col :span="16" style="font-size: 10px">
          <el-carousel :interval="4000" type="card" height="100px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img
                :src="item.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      records: [],
      total: 10,
      show: false,
      skuInfo: {},
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    //获取sku列表数据
    async getSkuList() {
      const { page, limit } = this;
      let result = await this.$API.sku.reqSkuLists(page, limit);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //当前页发生改变  触发
    handleCurrentChange(page) {
      this.page = page;
      this.getSkuList();
    },
    //当每一页展示数据量改变   触发
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //点击上架按钮 使其上架
    async upSale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message({
          type: "success",
          message: `"${row.skuName}"上架成功`,
        });
      } else {
        this.$message.error(`"${row.skuName}"上架失败`);
      }
    },
    //点击下架按钮 使其下架
    async downSale(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message({
          type: "success",
          message: `"${row.skuName}"下架成功`,
        });
      } else {
        this.$message.error(`"${row.skuName}"下架失败`);
      }
    },
    //点击编辑按钮
    explore() {
      this.$message("正在努力开发中");
    },
    //点击info按钮 触发
    async detail(row) {
      this.show = true;
      //获取sku数据
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code === 200) {
        this.skuInfo = result.data;
      }
    },
  },
};
</script>

<style>
.el-carousel__item {
  width: 100px;
}
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>